package com.yvtian.admin.controller;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yvtian.admin.pojo.SysDictDataEntity;
import com.yvtian.admin.pojo.SysDictTypeEntity;
import com.yvtian.admin.service.SysDictDataService;
import com.yvtian.admin.service.SysDictTypeService;
import com.yvtian.annotation.Log;
import com.yvtian.entity.Result;
import org.springframework.web.bind.annotation.*;
import com.yvtian.entity.StatusCode;
import com.yvtian.enums.OperatorType;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

/**
 * 字典类型表(sys_dict_type)表控制层
 *
 * @author long
 * @since 2024-04-16 16:06:01
 */
@RestController
@RequestMapping("sysDictType")
public class SysDictTypeController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictTypeService sysDictTypeService;
    @Resource
    private SysDictDataService sysDictDataService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return new Result(true,"查询成功", StatusCode.OK,this.sysDictTypeService.getById(id));
    }

    @GetMapping(value = "/list")
    public Result findByPage(Page<SysDictTypeEntity> page, SysDictTypeEntity sysDictTypeEntity) {
        return new Result(true, "查询成功", StatusCode.OK, sysDictTypeService.findByPage(page,sysDictTypeEntity));
    }

    /**
     * 新增数据
     *
     * @param sysDictTypeEntity 实体对象
     * @return 新增结果
     */
    @Log(value = "添加数据",isSave = true,operType = OperatorType.INSERT)
    @PostMapping("insert")
    public Result insert(@RequestBody SysDictTypeEntity sysDictTypeEntity) {
        return new Result(true,"插入成功", StatusCode.OK,this.sysDictTypeService.save(sysDictTypeEntity));
    }

    /**
     * 修改数据
     *
     * @param sysDictTypeEntity 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(@RequestBody SysDictTypeEntity sysDictTypeEntity) {
        return new Result(true,"更新成功", StatusCode.OK,this.sysDictTypeService.updateById(sysDictTypeEntity));
    }

    /**
     * 删除数据
     *
     * @param idMap 主键结合
     * @return 删除结果
     */
    @PostMapping("/del")
    public Result delete(@RequestBody Map<String, ArrayList<Integer>> idMap) {
        ArrayList<Integer> ids = idMap.get("ids");
        LambdaUpdateWrapper<SysDictDataEntity> wrapper = new LambdaUpdateWrapper<SysDictDataEntity>();
        wrapper.in(SysDictDataEntity::getDictCode,ids);
        sysDictDataService.remove(wrapper);
        return new Result(true, "删除成功", StatusCode.OK, this.sysDictTypeService.removeByIds(ids));
    }
}

